package mskruch.gps.experimental;


import gpstools.math.GeoCalculations;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import mskruch.gps.Point;
import mskruch.gps.Position;

/**
 * 
 * @author Slawomir.Przednowek
 * 
 */
public class SimpleCalculator {

    private List<Point> pointList = null;

    public void setPoints(Point[] points) {
        pointList = Arrays.asList(points);
    }
    
    public SimpleCalculator(Point[] points) {
        setPoints(points);
    }

    public double getDistance() {
        List<Point> extractedPointList = pointList;

        Position previousPoint = null;
        double totalDist = 0.0;
        for (Iterator<Point> iterator = extractedPointList.iterator(); iterator.hasNext();) {
            Position currentPoint = (Position) iterator.next();
            if (previousPoint == null) {
                previousPoint = currentPoint;
            } else {
                double currDist = GeoCalculations.distance(previousPoint.getLatitude(), previousPoint.getLongitude(),
                        currentPoint.getLatitude(), currentPoint.getLongitude());
                totalDist += currDist;
                previousPoint = currentPoint;
            }
        }

        double totalAscent = getElevationGain();
        double totalDescent = getElevationLoss();
        return Math.sqrt(totalDist * totalDist + Math.pow((totalAscent + totalDescent) / 1000.0, 2.0));
    }

    public double getElevationGain() {
        List<Point> extractedPointList = pointList;

        Point previousPoint = null;
        double totalAscent = 0.0;
        for (Iterator<Point> iterator = extractedPointList.iterator(); iterator.hasNext();) {
            Point currentPoint = (Point) iterator.next();
            if (previousPoint == null) {
                previousPoint = currentPoint;
            } else {
                double currAscent = currentPoint.getElevation() - previousPoint.getElevation();
                if (currAscent > 0) {
                    totalAscent += currAscent;
                }

                previousPoint = currentPoint;
            }
        }

        return totalAscent;
    }

    public double getElevationLoss() {
        List<Point> extractedPointList = pointList;

        Point previousPoint = null;
        double totalDescent = 0.0;
        for (Iterator<Point> iterator = extractedPointList.iterator(); iterator.hasNext();) {
            Point currentPoint = (Point) iterator.next();
            if (previousPoint == null) {
                previousPoint = currentPoint;
            } else {
                double currDescent = previousPoint.getElevation() - currentPoint.getElevation();
                if (currDescent > 0) {
                    totalDescent += currDescent;
                }

                previousPoint = currentPoint;
            }
        }

        return totalDescent;
    }

}
